/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filecopier.gui.scripteditor;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

/**
 *
 * @author david
 */
public class ButtonPanel extends JPanel implements PropertyChangeListener {

    private JButton newBtn = null;
    private JButton openBtn = null;
    private JButton saveBtn = null;
    private JButton saveAsBtn = null;
    private JButton saveAsTempBtn = null;
    private JButton deleteBtn = null;
    private JButton closeBtn = null;

    public ButtonPanel() {
        super();
        initialize();
    }

    public void addBtnActionListener(final ActionListener al) {
        newBtn.addActionListener(al);
        openBtn.addActionListener(al);
        saveBtn.addActionListener(al);
        saveAsBtn.addActionListener(al);
        saveAsTempBtn.addActionListener(al);
        deleteBtn.addActionListener(al);
        closeBtn.addActionListener(al);
    }

    public void addBtnActionListener(final ActionListener al, final String btnText) {
        if (btnText.equals(newBtn.getText())) {
            newBtn.addActionListener(al);
        } else if (btnText.equals(openBtn.getText())) {
            openBtn.addActionListener(al);
        } else if (btnText.equals(saveBtn.getText())) {
            saveBtn.addActionListener(al);
        } else if (btnText.equals(saveAsBtn.getText())) {
            saveAsBtn.addActionListener(al);
        } else if (btnText.equals(saveAsBtn.getText())) {
            saveAsBtn.addActionListener(al);
        } else if (btnText.equals(deleteBtn.getText())) {
            deleteBtn.addActionListener(al);
        } else if (btnText.equals(closeBtn.getText())) {
            closeBtn.addActionListener(al);
        }
    }

    private void initialize() {
        GridLayout gridLayout = new GridLayout(7, 1);
        gridLayout.setVgap(5);
        this.setLayout(gridLayout);
        this.setBorder(BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        this.add(getNewBtn(), null);
        this.add(getOpenBtn(), null);
        this.add(getSaveBtn(), null);
        this.add(getSaveAsBtn(), null);
        this.add(getSaveAsTempBtn(), null);
        this.add(getDeleteBtn(), null);
        this.add(getCloseBtn(), null);
    }

    private JButton getNewBtn() {
        if (newBtn == null) {
            newBtn = new JButton("New");
            newBtn.setMnemonic(KeyEvent.VK_N);
            newBtn.setToolTipText("Create new copy script.");
        }
        return newBtn;
    }

    private JButton getOpenBtn() {
        if (openBtn == null) {
            openBtn = new JButton("Open");
            openBtn.setMnemonic(KeyEvent.VK_O);
            openBtn.setToolTipText("Browse for copy script.");
        }
        return openBtn;
    }

    private JButton getSaveBtn() {
        if (saveBtn == null) {
            saveBtn = new JButton("Save");
            saveBtn.setMnemonic(KeyEvent.VK_S);
            saveBtn.setToolTipText("Save copy script to disk.");
            saveBtn.setEnabled(false);
        }
        return saveBtn;
    }

    private JButton getSaveAsBtn() {
        if (saveAsBtn == null) {
            saveAsBtn = new JButton("Save As");
            saveAsBtn.setMnemonic(KeyEvent.VK_A);
            saveAsBtn.setToolTipText("Save copy script as.");
            saveAsBtn.setEnabled(false);
        }
        return saveAsBtn;
    }

    private JButton getSaveAsTempBtn() {
        if (saveAsTempBtn == null) {
            saveAsTempBtn = new JButton("Save As Temp");
            saveAsTempBtn.setMnemonic(KeyEvent.VK_T);
            saveAsTempBtn.setToolTipText("Save script file in temporary folder. Temporary files are deleted after successful use.");
            saveAsTempBtn.setEnabled(false);
        }
        return saveAsTempBtn;
    }

    private JButton getDeleteBtn() {
        if (deleteBtn == null) {
            deleteBtn = new JButton("Delete");
            deleteBtn.setMnemonic(KeyEvent.VK_D);
            deleteBtn.setToolTipText("Delete script file.");
        }
        return deleteBtn;
    }

    private JButton getCloseBtn() {
        if (closeBtn == null) {
            closeBtn = new JButton("Close");
            closeBtn.setMnemonic(KeyEvent.VK_C);
            closeBtn.setToolTipText("Close script editor.");
        }
        return closeBtn;
    }

    public void propertyChange(PropertyChangeEvent e) {
        Boolean enable = (Boolean) e.getNewValue();
        saveBtn.setEnabled(enable);
        saveAsBtn.setEnabled(enable);
        saveAsTempBtn.setEnabled(enable);
    }
}
